package com.leixin.demo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.leixin.demo.dao.PostData;
import com.leixin.demo.entity.UrlEntity;
import com.leixin.demo.service.UrlService;
import com.leixin.demo.utlis.ThreadRunnable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.*;

@RestController
@RequestMapping
public class IndexController {
    @Autowired
    UrlService urlService;

    @PostMapping(value = "/selectall")
    public PageInfo selectall(@RequestBody PostData postData) {
        PageHelper.startPage(postData.getPageIndex(), postData.getPageSize());
        if (StringUtil.isNotEmpty(postData.getName())) {
            UrlEntity url = new UrlEntity();
            url.setName(postData.getName());
            List<UrlEntity> selectname = urlService.selectname(url);
            PageInfo p = new PageInfo(selectname);
            return p;
        }
        List<UrlEntity> selectall = urlService.selectall();
        PageInfo p = new PageInfo(selectall);
        return p;
    }

    @GetMapping(value = "/avi")
    public String avi(Integer start,Integer end){
//        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, size, 2000, TimeUnit.MILLISECONDS,
//                new ArrayBlockingQueue<Runnable>(5));
//        for(int i=0;i<size;i++){
//            ThreadRunnable myTask = new ThreadRunnable(i);
//            executor.execute(myTask);
//            System.out.println("线程池中线程数目："+executor.getPoolSize()+"，队列中等待执行的任务数目："+
//                    executor.getQueue().size()+"，已执行玩别的任务数目："+executor.getCompletedTaskCount());
//        }
//        executor.shutdown();
//        ExecutorService pool = Executors.newFixedThreadPool(5);
        ExecutorService pool = Executors.newFixedThreadPool(5);
        for (int i=start;i<end+1;i++){
            pool.execute(new ThreadRunnable(i));
        }
        return "获取链接完毕";
    }

    @GetMapping(value = "/getImg")
    public String pathImg(String path) throws Exception {
        urlService.pathImg(path);
        return "获取链接完毕";
    }

    @GetMapping(value = "/ping")
    public String ping(){
        return "连接通畅";
    }
}